{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在Python中，能够直接处理的数据类型有以下几种\n",
    "\n",
    "### 一 整数\n",
    "\n",
    "Python可以处理任意大小的整数，当然包括负整数，在Python程序中，整数的表示方法和数学上的写法一模一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a1 = 1\n",
    "a2 = 100\n",
    "a3 = -80\n",
    "a4 = 0\n",
    "print(a1, a2, a3, a4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以使用二进制，所以，有时候用十六进制表示整数比较方便，十六进制用 “0x” 前缀和0-9，a-f表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 十六进制\n",
    "b1 = 0xff00\n",
    "b2 = 0xa5bc3\n",
    "b3 = 0xA\n",
    "print(b1, b2, b3)\n",
    "\n",
    "# 二进制\n",
    "b4 = 0b1101\n",
    "# 八进制\n",
    "b5 = 0o177\n",
    "print(b4, b5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二 浮点数\n",
    "\n",
    "浮点数也就是小数，之所以称为浮点数，是因为按照科学记数法表示时，一个浮点数的小数点位置是可变的，\n",
    "\n",
    "比如，1.23x10^9和12.3x10^8是相等的。浮点数可以用数学写法，如1.23，3.14，-9.01，等等。\n",
    "\n",
    "但是对于很大或很小的浮点数，就必须用科学计数法表示，把10用e替代，\n",
    "\n",
    "1.23x10^9就是1.23e9，12.3e8，\n",
    "\n",
    "0.000012可以写成1.2e-5，等等。\n",
    "\n",
    "整数和浮点数在计算机内部存储的方式是不同的，整数运算永远是精确的，而浮点数运算则可能会有四舍五入的误差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "c1 = 12.3\n",
    "c2 = -9.01\n",
    "c3 = 12.3e9\n",
    "c4 = 1.2e-05\n",
    "print(c1, c2, c3, c4)\n",
    "print('强制科学计数法: %e' % c3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三 字符串\n",
    "\n",
    "字符串是以''或 \"\" 括起来的任意文本，比如'abc'，\"xyz\"等等。\n",
    "\n",
    "请注意，''或\"\"本身只是一种表示方式，不是字符串的一部分，因此，字符串'abc' 只有a，b，c这3个字符。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "d1 = 'abc'\n",
    "d2 = \"edf\"\n",
    "d3 = 'a\"b\"c'\n",
    "d4 = \"\"\"第一行 “abc”\n",
    "第二行 ‘edf’\n",
    "\"\"\"\n",
    "print('d1=',d1)\n",
    "print('d2=',d2)\n",
    "print('d3=',d3)\n",
    "print('d4=',d4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 延伸知识：Unicode\n",
    "\n",
    "Unicode通常用两个字节表示一个字符，原有的英文编码从单字节变成双字节，只需要把高字节全部填为0就可以。\n",
    "\n",
    "因为Python的诞生比Unicode标准发布的时间还要早，所以最早的Python只支持ASCII编码，普通的字符串'ABC'在Python内部都是ASCII编码的。\n",
    "\n",
    "Python在后来添加了对Unicode的支持，以Unicode表示的字符串用u'...'表示\n",
    "\n",
    "\n",
    "<strong>Python3.x默认就是Unicode</strong>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 四 布尔值\n",
    "\n",
    "布尔值和布尔代数的表示完全一致，一个布尔值只有True、False两种值，要么是True，要么是False，\n",
    "\n",
    "在Python中，可以直接用True、False表示布尔值（<b>请注意大小写</b>），也可以通过布尔运算计算出来。\n",
    "\n",
    "布尔值可以用and、or和not运算。\n",
    "\n",
    "#### and运算是与运算\n",
    "只有所有都为 True，and运算结果才是 True。\n",
    "\n",
    "#### or运算是或运算\n",
    "只要其中有一个为 True，or 运算结果就是 True。\n",
    "\n",
    "#### not运算是非运算\n",
    "它是一个单目运算符，把 True 变成 False，False 变成 True。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "e1 = True\n",
    "e2 = False\n",
    "e3 = e1 and e2\n",
    "e4 = e1 or e2\n",
    "e5 = not e4\n",
    "print(\"e1={0}, e2={1}, e3={2}, e4={3}, e5={4}\".format(e1,e2,e3,e4,e5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 五 空值\n",
    "空值是Python里一个特殊的值，用None表示。\n",
    "<b>None不能理解为0</b>，因为0是有意义的，而None是一个特殊的空值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f1 = None\n",
    "print(f1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 计算十进制整数 678 和十六进制整数 0x12fd 之和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 计算二进制整数 0b111 和八进制整数 0o12 之和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 请用字符串表示出 Learn Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 请用字符串表示出 ''I am the \"best\" ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 计算以下表达式的布尔值（注意==表示判断是否相等）\n",
    "# 100 < 99\n",
    "# 0x99 == 99"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 语法解释\n",
    "\n",
    "### print\n",
    "\n",
    "print语句可以向屏幕上输出指定的文字\n",
    "\n",
    "print语句也可以跟上多个字符串，用逗号“,”隔开，就可以连成一串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a1 = 1\n",
    "a2 = 100\n",
    "a3 = -80\n",
    "a4 = 0\n",
    "print(a1, a2, a3, a4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python的注释\n",
    "\n",
    "任何时候，我们都可以给程序加上注释。注释是用来说明代码的，给自己或别人看，而程序运行的时候，Python解释器会直接忽略掉注释，所以，有没有注释不影响程序的执行结果，但是影响到别人能不能看懂你的代码。\n",
    "\n",
    "Python的注释以 ** # ** 开头，后面的文字直到行尾都算注释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 我是注释\n",
    "print('注释')  # 我也是注释\n",
    "# print('我不行运行')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### import\n",
    "\n",
    "Python语言中import的使用很简单，直接使用import module_name语句导入即可。这里我主要写一下\"import\"的本质\n",
    "\n",
    "模块（module）：用来从逻辑（实现一个功能）上组织Python代码（变量、函数、类），本质就是*.py文件。文件是物理上组织方式\"module_name.py\"，模块是逻辑上组织方式\"module_name\"。\n",
    "\n",
    "包（package）：定义了一个由模块和子包组成的Python应用程序执行环境，本质就是一个有层次的文件目录结构（必须带有一个__init__.py文件）\n",
    "\n",
    "#### 使用适当的模块，可以大量减少代码的编写，这也是Python一个大的优点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 引入数学模块　math\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(math.pi)  # 圆周率\n",
    "print(math.degrees(math.pi/4))  # 转为度数\n",
    "print(math.pow(2, 4))  # 返回x的y次方，即x**y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 遇到不懂，怎样查找答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Help\n",
    "# math的功能\n",
    "help(math)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "l = []\n",
    "help(l.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# dir\n",
    "dir(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 善用搜索引擎\n",
    "\n",
    "百度，bing，谷歌 ： python + 库名字 （例子）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
